package com.tiheima.aop;

import com.tiheima.service.OperationLogService;
import com.tiheima.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class OperationLogAspect {

    @Resource
    private OperationLogService operationLogService;


    @Pointcut(value = "@annotation(com.tiheima.aop.anno.OperationLogAnno)")
    public void log(){}


    @Around("log()")
    public Object  logAdvice(ProceedingJoinPoint pjp) throws Throwable {
        // 1. 补全方法执行时间属性
        long begin = System.currentTimeMillis();
        /// /////////////////////////////////////////////////////////
        // 执行目标方法
        Object returnValue = pjp.proceed();
        /////////////////////////////////////////////////////////////
        long end = System.currentTimeMillis();
        long costTime = end - begin;
        // 1.7 获取操作人id
        // 从ThreadLocal中获取
        Integer empId = ThreadLocalUtil.get();
        operationLogService.insertLog(pjp,returnValue,empId,costTime);

        System.out.println("AOP切面Aspect线程的名称"+Thread.currentThread().getName());

        return returnValue;
    }
}
